package org.bwgl.demo6.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import java.io.File;
import java.io.IOException;

@RestController
public class UpFile {
    @PostMapping(value = "up")
    public String up(String nickname, MultipartFile photo, HttpServletRequest request) throws IOException {
        System.out.println("nickname="+nickname);
        System.out.println("photo="+photo.getOriginalFilename());
        System.out.println("photo="+photo.getContentType());

        String realPath = request.getServletContext().getRealPath("/up");
        System.out.println(realPath);
        saveFile(photo,realPath);
    return "ok";
    }

    private void saveFile(MultipartFile photo, String realPath) throws IOException {
        File path = new File(realPath);
        if (!path.exists()){
            path.mkdir();
        }
        File file = new File(realPath+"\\"+photo.getOriginalFilename());
        photo.transferTo(file);
    }
}
